from django.shortcuts import render, redirect

from app01.models import Departement


def dept_list(request):
    """部门列表"""
    queryset = Departement.objects.all()
    return render(request, 'depart_list.html', {'queryset':queryset})

def dept_add(request):
    """添加部门"""
    if request.method == "GET":
        return render(request, 'depart_add.html')
    # 如果是POST请求 获取表单中的数据
    title = request.POST.get('title')
    # 保存到数据库中
    Departement.objects.create(title=title)

    return redirect('/dept/list')

def dept_delete(request,uid):
    """删除部门"""
    Departement.objects.filter(id=uid).delete()
    # 重定向到列表
    return redirect('/dept/list')

def dept_update(request,uid):
    """修改部门信息"""
    if request.method == "GET":
        # 从数据库中查询出数据，并数据回显
        row_obj = Departement.objects.filter(id=uid).first()
        print(row_obj)
        return render(request, 'depart_update.html', {'row_obj':row_obj})
    # POST请求的话获取 页面中的信息保存到数据库
    title = request.POST.get('title')
    Departement.objects.filter(id=uid).update(title=title)
    return redirect('/dept/list')